#import "SEGWinningrecorddetailManager.h"

#if __has_include(<Commom/SEGModel.h>)
#import <Commom/SEGModel.h>
#else
#import "SEGModel.h"
#endif
#if __has_include(<Commom/SEGHTTPBusinessManager.h>)
#import <Commom/SEGHTTPBusinessManager.h>
#else
#import "SEGHTTPBusinessManager.h"
#endif

static id _winningRecordDetailManager;
static dispatch_once_t onceToken;

@implementation SEGWinningrecorddetailManager
#pragma mark - SINGLE----BEGIN----
+(instancetype)sharedManager{
    dispatch_once(&onceToken, ^{
        _winningRecordDetailManager = [[self alloc] init];
    });
    return _winningRecordDetailManager;
}

+(void)destroyResource{
    onceToken = 0;
    _winningRecordDetailManager = nil;
}

#pragma mark - SINGLE----END----

#pragma mark - HTTP----BEGIN----

/// 中奖记录详情接口
/// @param recordId 记录ID
-(void)requestWinningrecorddetailWithRecordid:(NSString*)recordId finished:(void(^)(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage))finished{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:recordId forKey:@"recordId"];
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/cms-api/example/winningRecordDetail",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:params completed:^(BOOL isSuccessed, id result, NSString *codeType, NSError *error) {
        if (!isSuccessed) {
            finished(isSuccessed,nil,codeType,error.localizedDescription);
            return;
        }
        
        self.winningRecordDetail = [SEGWinningrecorddetailModel yy_modelWithDictionary:result];
        
        // {"addressee":"收件人","tel":"手机号","address":"地址"}
        if (self.winningRecordDetail.data.winnerDetail.length > 0) {
            NSDictionary *winnerDetail = [SEGWinningrecorddetailManager stringToJson:self.winningRecordDetail.data.winnerDetail];
            if (winnerDetail && [winnerDetail isKindOfClass:[NSDictionary class]]) {
                self.winningRecordDetail.data.wTel = [winnerDetail valueForKey:@"tel"];
                self.winningRecordDetail.data.addressee = [winnerDetail valueForKey:@"addressee"];
                self.winningRecordDetail.data.address = [winnerDetail valueForKey:@"address"];
            }
        }
        
        // {"tel":"手机号","courierCompany":"快递公司","shipmentNumber":"物流单号","redemptionCode":"兑换码","cardNumber":"卡号","password":"密码"}
        if (self.winningRecordDetail.data.issueDetail.length > 0) {
            NSDictionary *issueDetail = [SEGWinningrecorddetailManager stringToJson:self.winningRecordDetail.data.issueDetail];
            if (issueDetail && [issueDetail isKindOfClass:[NSDictionary class]]) {
                self.winningRecordDetail.data.tel = [issueDetail valueForKey:@"tel"];
                self.winningRecordDetail.data.courierCompany = [issueDetail valueForKey:@"courierCompany"];
                self.winningRecordDetail.data.shipmentNumber = [issueDetail valueForKey:@"shipmentNumber"];
                self.winningRecordDetail.data.redemptionCode = [issueDetail valueForKey:@"redemptionCode"];
                self.winningRecordDetail.data.cardNumber = [issueDetail valueForKey:@"cardNumber"];
                self.winningRecordDetail.data.password = [issueDetail valueForKey:@"password"];
            }
        }
        
        finished(isSuccessed,result,codeType,error.localizedDescription);
        return;
    }];
}

+(NSDictionary*)stringToJson:(NSString*)stringValue{
    NSData *jsonData = [stringValue dataUsingEncoding:NSUTF8StringEncoding];
    if (!jsonData)  return nil;
    NSDictionary *queryDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:NULL];
    if (!queryDic)  return nil;
    return queryDic;
}
#pragma mark - HTTP----END----

#pragma mark - GETTER----BEGIN----
-(SEGWinningrecorddetailModel *)winningRecordDetail{
    if (!_winningRecordDetail) {
        _winningRecordDetail = [[SEGWinningrecorddetailModel alloc]init];
    }
    return _winningRecordDetail;
}
#pragma mark - GETTER----END----
@end
